package com.spring.arch.common.core.configuration;

import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.TcpIpConfig;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

/**
 * Hazelcast缓存服务端配置
 * 
 * @author Frank
 * @version 1.0.0.1
 * @since JDK 1.8
 */
@Setter
@Getter
public class HazelcastServerProperties {
    private String instanceName;
    private String groupName;
    private String groupPassword = "groupPassword@dymmy1!";

    private boolean managementCenterEnabled = false;
    private String managementCenterUrl = "http://hazelcast:8080/hazelcast-mancenter";
    private int managementCenterUpdateInterval = 5;

    /// private String interfaces;
    private String members;
    private Integer port;

    public HazelcastServerProperties(final String groupName, final Integer port) {
        this.groupName = groupName;
        this.port = port;
    }

    public NetworkConfig buildNetworkConfig() {
        final NetworkConfig networkConfig = new NetworkConfig();
        networkConfig.setPort(this.port);
        // if (port != null) {
        // networkConfig.setPort(port);
        // return networkConfig;
        // }
        //
        // if (StringUtils.hasText(interfaces)) {
        // final String[] interfaceArray = interfaces.split(",");
        // final InterfacesConfig interfacesConfig = networkConfig.getInterfaces().setEnabled(true);
        // for (final String ip : interfaceArray) {
        // interfacesConfig.addInterface(ip);
        // }
        // return networkConfig;
        // }

        networkConfig.getJoin().getMulticastConfig().setEnabled(false);
        if (StringUtils.hasText(this.members)) {
            // final MulticastConfig multicastConfig = networkConfig.getJoin().getMulticastConfig();
            // final String[] trustedInterfacesArray = trustedInterfaces.split(",");
            // for (final String ip : trustedInterfacesArray) {
            // multicastConfig.addTrustedInterface(ip);
            // }
            final TcpIpConfig tcpIpConfig = networkConfig.getJoin().getTcpIpConfig().setEnabled(true);
            final String[] memberArray = this.members.split(",");
            for (final String member : memberArray) {
                tcpIpConfig.addMember(member);
            }
            return networkConfig;
        }
        return networkConfig;
    }
}
